package com.sanhe.rfmuscle.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.menu.app.db.bean.CusUsage;
import com.sanhe.rfmuscle.R;
import com.sanhe.rfmuscle.databinding.ParamGentleViewBinding;
import com.sanhe.rfmuscle.databinding.ParamListItem2Binding;
import com.sanhe.rfmuscle.databinding.ParamProViewBinding;
import com.sanhe.rfmuscle.databinding.ParamSportViewBinding;
import com.sanhe.rfmuscle.databinding.ParamView1Binding;
import com.sanhe.rfmuscle.view.ParamDlg;

import java.util.HashMap;
import java.util.List;

public class ParamAdapter extends RecyclerView.Adapter {

    List<CusUsage> list;
    ParamDlg dlg;
    Context context;

    public ParamAdapter(Context context, ParamDlg dlg, List list) {
        this.context = context;
        this.list = list;
        this.dlg = dlg;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ParamListItem2Binding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.param_list_item2, parent, false);
        ItemHolder holder = new ItemHolder(binding);
        ParamView1Binding bind1 = ParamView1Binding.inflate(LayoutInflater.from(binding.getRoot().getContext()), binding.workFrame, false);
        binding.workFrame.addView(bind1.getRoot(), 0);
        holder.viewBinding1 = bind1;
        if (viewType == 1) {
            ParamSportViewBinding tmp = ParamSportViewBinding.inflate(LayoutInflater.from(binding.getRoot().getContext()), binding.adjustFrame, false);
            binding.adjustFrame.addView(tmp.getRoot(), 0);
            holder.viewBinding2 = tmp;
        } else if (viewType == 2) {
            ParamGentleViewBinding tmp = ParamGentleViewBinding.inflate(LayoutInflater.from(binding.getRoot().getContext()), binding.adjustFrame, false);
            binding.adjustFrame.addView(tmp.getRoot(), 0);
            holder.viewBinding2 = tmp;
        } else {
            ParamProViewBinding tmp = ParamProViewBinding.inflate(LayoutInflater.from(binding.getRoot().getContext()), binding.adjustFrame, false);
            binding.adjustFrame.addView(tmp.getRoot(), 0);
            holder.viewBinding2 = tmp;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((ItemHolder) holder).bind(list.get(position), position);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        String mode = list.get(position).info.get("mode") + "";
        if ("1".equals(mode)) {
            return 1;
        } else if ("2".equals(mode)) {
            return 2;
        } else {
            return 3;
        }
    }

    class ItemHolder extends RecyclerView.ViewHolder {

        ParamListItem2Binding binding;
        public Object viewBinding1;
        public Object viewBinding2;

        public ItemHolder(ParamListItem2Binding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(CusUsage data, int position) {
            if (data.info != null && !data.info.isEmpty()) {
                binding.mode.setText(getMode(data.info));
                ((ParamView1Binding) viewBinding1).time1.setText(data.info.get("time1") + "");
                ((ParamView1Binding) viewBinding1).time2.setText(data.info.get("time2") + "");
                ((ParamView1Binding) viewBinding1).fluence1.setText(data.info.get("fluence1") + "");
                ((ParamView1Binding) viewBinding1).fluence2.setText(data.info.get("fluence2") + "");
                if (data.info.get("mode").equals("1")){
                    ((ParamView1Binding) viewBinding1).rf1.setText("/");
                    ((ParamView1Binding) viewBinding1).rf2.setText("/");
                } else {
                    ((ParamView1Binding) viewBinding1).rf1.setText(data.info.get("rf1") + "");
                    ((ParamView1Binding) viewBinding1).rf2.setText(data.info.get("rf2") + "");
                }
                if (viewBinding2 instanceof ParamSportViewBinding) {
                    ((ParamSportViewBinding) viewBinding2).sportMode.setText("/");
                } else if (viewBinding2 instanceof ParamGentleViewBinding) {
                    ((ParamGentleViewBinding) viewBinding2).gentleMode.setText("/");
                } else if (viewBinding2 instanceof ParamProViewBinding) {
                    ((ParamProViewBinding) viewBinding2).main.setText(data.info.get("mf") + "");
                    ((ParamProViewBinding) viewBinding2).mainTime.setText(data.info.get("mt") + "");
                    ((ParamProViewBinding) viewBinding2).sub.setText(data.info.get("sf") + "");
                    ((ParamProViewBinding) viewBinding2).subTime.setText(data.info.get("st") + "");
                }
                binding.count.setText(data.point + "");
            } else {
                binding.mode.setText("\\");
                binding.count.setText("\\");
            }

            binding.index.setText(String.valueOf(position + 1));
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dlg.toWork(data);
                }
            });
        }
    }

    public String getMode(HashMap<String, String> map) {
        try {
            String s = map.get("mode");
            int type = Integer.valueOf(s);
            if (type == 1) {
                return context.getResources().getString(R.string.sport);
            } else if (type == 2) {
                return context.getResources().getString(R.string.gentle);
            } else if (type == 3) {
                return context.getResources().getString(R.string.pro2);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return "--";

    }
}

